package club.emergency.wechat.department.operate.consumer.controller;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.department.operate.consumer.service.DepartmentOperateConsumer;
import club.emergency.wechat.model.department.Department;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.department.operate.consumer.controller
 * Date: 2019/3/21
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@Api("操作企业接口")
@RestController
@RequestMapping("/department-operate")
public class DepartmentOperateController {

    private DepartmentOperateConsumer departmentOperateConsumer;

    public DepartmentOperateController(DepartmentOperateConsumer departmentOperateConsumer) {
        this.departmentOperateConsumer = departmentOperateConsumer;
    }

    @ApiOperation("新增企业数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentName", value = "企业名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "parentCode", value = "父code", dataType = "string", paramType = "query")
    })
    @PostMapping("/save")
    public ResponseEntity save(
            @RequestParam("departmentName") String departmentName,
            @RequestParam(value = "parentId", required = false) Long parentId,
            @RequestParam(value = "parentCode", required = false) String parentCode
    ) {
        Department department = new Department();
        department.setDepartmentName(departmentName);
        department.setParentId(parentId);
        return new JsonConvert<ResponseEntity>().jsonDecode(departmentOperateConsumer.save(department, parentCode), ResponseEntity.class);
    }

    @ApiOperation("修改企业数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "departmentName", value = "企业名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "parentCode", value = "父code", dataType = "string", paramType = "query")
    })
    @PostMapping("/modify")
    public ResponseEntity modify(
            @RequestParam("id") Long id,
            @RequestParam("departmentName") String departmentName,
            @RequestParam("parentCode") String parentCode
    ) {
        Department department = new Department();
        department.setId(id);
        department.setDepartmentName(departmentName);
        return new JsonConvert<ResponseEntity>().jsonDecode(departmentOperateConsumer.modify(department, parentCode), ResponseEntity.class);
    }

    @ApiOperation("删除企业数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long", paramType = "query")
    })
    @PostMapping("/remove")
    public ResponseEntity remove(
            @RequestParam("id") Long id
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(departmentOperateConsumer.remove(id), ResponseEntity.class);
    }
}
